<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>git进阶命令 | 若愚湖畔</title><meta name="keywords" content="git"><meta name="author" content="albafica"><meta name="copyright" content="albafica"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="git 补充命令git用了很久，但更多停留在push pull这些基本命令上，今天学习了几个进阶命令，记录一下。 git reset世界上没有后悔药，但git reset就是代码里面的后悔药，通过这个命令可以还原已经提交的代码。  命令列表   git reset --hard 可以将代码还原到指定分支，中间的提交记录全部舍弃 git reset --soft 可以将代码还原到指定分支，同时保留中">
<meta property="og:type" content="article">
<meta property="og:title" content="git进阶命令">
<meta property="og:url" content="https://albafica.gitee.io/2022/04/23/git-command/index.html">
<meta property="og:site_name" content="若愚湖畔">
<meta property="og:description" content="git 补充命令git用了很久，但更多停留在push pull这些基本命令上，今天学习了几个进阶命令，记录一下。 git reset世界上没有后悔药，但git reset就是代码里面的后悔药，通过这个命令可以还原已经提交的代码。  命令列表   git reset --hard 可以将代码还原到指定分支，中间的提交记录全部舍弃 git reset --soft 可以将代码还原到指定分支，同时保留中">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="">
<meta property="article:published_time" content="2022-04-23T13:38:00.000Z">
<meta property="article:modified_time" content="2022-04-23T13:38:00.882Z">
<meta property="article:author" content="albafica">
<meta property="article:tag" content="git">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content=""><link rel="shortcut icon" href="/hexoblog/img/favicon.png"><link rel="canonical" href="https://albafica.gitee.io/2022/04/23/git-command/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="stylesheet" href="/hexoblog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/hexoblog/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":200},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'git进阶命令',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-04-23 21:38:00'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.1.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://s1.ax1x.com/2022/04/23/Lhpz3F.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/hexoblog/archives/"><div class="headline">文章</div><div class="length-num">11</div></a></div><div class="data-item"><a href="/hexoblog/tags/"><div class="headline">标签</div><div class="length-num">6</div></a></div><div class="data-item"><a href="/hexoblog/categories/"><div class="headline">分类</div><div class="length-num">2</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/hexoblog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/hexoblog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/hexoblog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/hexoblog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/hexoblog/">若愚湖畔</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/hexoblog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/hexoblog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/hexoblog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/hexoblog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">git进阶命令</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-04-23T13:38:00.000Z" title="发表于 2022-04-23 21:38:00">2022-04-23</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-04-23T13:38:00.882Z" title="更新于 2022-04-23 21:38:00">2022-04-23</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/hexoblog/categories/%E5%B7%A5%E5%85%B7/">工具</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.2k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>3分钟</span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="git-补充命令"><a href="#git-补充命令" class="headerlink" title="git 补充命令"></a>git 补充命令</h2><p>git用了很久，但更多停留在<code>push</code> <code>pull</code>这些基本命令上，今天学习了几个进阶命令，记录一下。</p>
<h3 id="git-reset"><a href="#git-reset" class="headerlink" title="git reset"></a>git reset</h3><p>世界上没有后悔药，但<code>git reset</code>就是代码里面的后悔药，通过这个命令可以还原已经提交的代码。</p>
<blockquote>
<p>命令列表</p>
</blockquote>
<ul>
<li><code>git reset --hard</code> 可以将代码还原到指定分支，中间的提交记录全部舍弃</li>
<li><code>git reset --soft</code> 可以将代码还原到指定分支，同时保留中间提交记录的修改内容</li>
</ul>
<p>上述两个命令，根据实际情况使用，再配合<code>git push -f</code>将分支强行推送到远端。</p>
<blockquote>
<p>长久的工作中，不可避免出现过误提交代码到远端的情况，之前简单了解过这个命令，但没深入研究，担心远端代码搞坏，所以都是选择版本比对重新产生新的commit。有这个命令，可以让提交树干净不少。</p>
</blockquote>
<h3 id="git-stash"><a href="#git-stash" class="headerlink" title="git stash"></a>git stash</h3><p>程序员每天的工作不是改bug就是在写bug的路上。所以不可避免，但遇到bug时，只能将自己手上没写完的bug放一放，优先去写bug。所以看git log都是一大堆的<code>代码暂存</code>之类的垃圾提交。<code>git stash</code>可以将当前暂存区内容保存起来，这时候当前分支就是一个干净的分支，可以切到其他分支改完bug后切回来，再恢复代码即可。</p>
<blockquote>
<p>命令列表</p>
</blockquote>
<ul>
<li><code>git stash save &quot;comment content&quot;</code> 保存未提交的代码</li>
<li><code>git stash list</code> 查看保存代码列表</li>
<li><code>git stash apply</code> 还原最近一次保存</li>
<li><code>git stash pop</code> 还原最近一次保存并删除</li>
<li><code>git stash clear</code> 删除所有保存代码</li>
<li><code>git stash drop</code> 删除最近一次保存</li>
<li>上述部分命令可以指定从list命令中查询出来的版本号</li>
</ul>
<h3 id="git-reflog"><a href="#git-reflog" class="headerlink" title="git reflog"></a>git reflog</h3><p><code>git log</code>只能查看当前commit的历史记录，如果使用了上面的<code>git reset</code>命令，将代码还原，但实际操作时，版本号提交错了还原多了，把不改丢弃的代码也丢弃了，这时候，log里面是没有记录的，所以可以通过<code>git reflog</code>这个记录查询所有提交记录，找一个后悔药再吃一顿。</p>
<ul>
<li><code>git log</code> 命令可以显示所有提交过的版本信息</li>
<li><code>git reflog</code> 可以查看所有分支的所有操作记录(包括已经被删除的 commit 记录和 reset 的操作)</li>
</ul>
<h3 id="cherry-pick"><a href="#cherry-pick" class="headerlink" title="cherry-pick"></a>cherry-pick</h3><blockquote>
<p>描述</p>
</blockquote>
<blockquote>
<p>给定一个或多个现有提交，应用每个提交引入的更改，为每个提交记录一个新的提交。这需要您的工作树清洁（没有从头提交的修改）。</p>
</blockquote>
<p>工作中不可避免的一个场景是排期问题，约定好两周后上线3个需求，现在老板突然说有一个需求明天就要上，怎么办？从develop拉一个新分支，然后从开发分支中吧代码挑出来放进新分支送测上线，等另一个分支送测时，先解决一下冲突。使用<code>cherry-pick</code>可以很好的解决这个问题，指定一个或多个commitid从另一个分支中吧代码转移过来就好了。这个命令还是比较复杂的，使用上还是要慎重一些。</p>
<h3 id="revert"><a href="#revert" class="headerlink" title="revert"></a>revert</h3><blockquote>
<p>描述</p>
</blockquote>
<blockquote>
<p>给定一个或多个现有提交，恢复相关提交引入的更改，并记录一些这些更改的新提交。这就要求你的工作树是干净的（没有来自头部的修改）。</p>
</blockquote>
<p>这个命令有点类似于reset，reset是版本回退，仿佛什么都没发生，revert是去掉中间的commit后重新提交，类似于删除代码后再commit一份删除后的代码上去，看git记录有提交-删除-在提交的日志在。使用场景上也非常好用，就是线上有一个功能有bug但一时半会找不到原因，由于中间还有其他人的提交记录，不能直接reset，只能revert掉自己的提交记录。</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>上述命令在日常工作中还是很有用的，但我觉得这些命令还基于一个前提，git的commit没有太多的提交，一个功能点在有限的commit能罗列清楚。但实际工作中这很多时候难以落地，一个功能前前后后可能垮了太多的提交，之前我们也会要求下班前commit上班后pull，避免电脑罢工一切白瞎。所以该学还是要学，但不能真拿这玩意当后悔药，有时候我个人觉得beyond compare比这后悔药安全。一家之言。</p>
<h2 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h2><p><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/MzEJoBaw3pMfHCRIPgVujA">Git 不要只会 pull 和 push，试试这 5 条提高效率的命令</a></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">albafica</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://albafica.gitee.io/2022/04/23/git-command/">https://albafica.gitee.io/2022/04/23/git-command/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://albafica.gitee.io" target="_blank">若愚湖畔</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/hexoblog/tags/git/">git</a></div><div class="post_share"></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://s1.ax1x.com/2022/04/23/Lhpz3F.png" onerror="this.onerror=null;this.src='/hexoblog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">albafica</div><div class="author-info__description">Stay hungry Stay foolish</div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/hexoblog/archives/"><div class="headline">文章</div><div class="length-num">11</div></a></div><div class="card-info-data-item"><a href="/hexoblog/tags/"><div class="headline">标签</div><div class="length-num">6</div></a></div><div class="card-info-data-item"><a href="/hexoblog/categories/"><div class="headline">分类</div><div class="length-num">2</div></a></div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#git-%E8%A1%A5%E5%85%85%E5%91%BD%E4%BB%A4"><span class="toc-number">1.</span> <span class="toc-text">git 补充命令</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#git-reset"><span class="toc-number">1.1.</span> <span class="toc-text">git reset</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#git-stash"><span class="toc-number">1.2.</span> <span class="toc-text">git stash</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#git-reflog"><span class="toc-number">1.3.</span> <span class="toc-text">git reflog</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#cherry-pick"><span class="toc-number">1.4.</span> <span class="toc-text">cherry-pick</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#revert"><span class="toc-number">1.5.</span> <span class="toc-text">revert</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">2.</span> <span class="toc-text">总结</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99"><span class="toc-number">3.</span> <span class="toc-text">参考资料</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/hexoblog/2022/05/18/techupdate/" title="技术升级简要构思">技术升级简要构思</a><time datetime="2022-05-18T12:54:43.000Z" title="发表于 2022-05-18 20:54:43">2022-05-18</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/hexoblog/2022/05/07/functionalinterface/" title="Function接口简单代码结构">Function接口简单代码结构</a><time datetime="2022-05-07T10:18:48.000Z" title="发表于 2022-05-07 18:18:48">2022-05-07</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/hexoblog/2022/05/07/cap/" title="微服务-CAP理论">微服务-CAP理论</a><time datetime="2022-05-07T01:12:07.000Z" title="发表于 2022-05-07 09:12:07">2022-05-07</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/hexoblog/2022/05/02/optional/" title="optional的使用">optional的使用</a><time datetime="2022-05-02T02:10:03.000Z" title="发表于 2022-05-02 10:10:03">2022-05-02</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/hexoblog/2022/05/01/pattern-overload/" title="设计模式-概述">设计模式-概述</a><time datetime="2022-05-01T10:12:20.000Z" title="发表于 2022-05-01 18:12:20">2022-05-01</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By albafica</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/hexoblog/js/utils.js"></script><script src="/hexoblog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><div class="js-pjax"></div><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-show-text.min.js" data-mobile="false" data-text="富强,民主,文明,和谐,爱国,敬业,诚信,友善" data-fontsize="15px" data-random="false" async="async"></script></div></body></html>